package org.itboys.mobile.controller.site;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.entity.mongo.JoinMessage;
import org.itboys.mobile.entity.mongo.JoinPicture;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mongo.site.JoinSiteCountService;
import org.itboys.mobile.service.mongo.site.JoinSiteMessageService;
import org.itboys.mobile.service.mongo.site.JoinSitePictureService;
import org.itboys.mobile.service.mongo.site.SundaySiteAdvertiseMentService;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mongo.web.SundayPermissionService;
import org.itboys.mobile.service.mysql.site.SundaySiteProductService;
import org.itboys.mobile.service.mysql.web.product.SundayProductCategoryService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/12
 * 描述：雄安加盟网_项目
 */
@Controller
@RequestMapping("/sunday/site/product")
public class SundaySiteProductController extends BaseController{
    @Autowired
    private SundaySiteProductService productService;
    @Autowired
    private SundayPermissionService permissionService;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private JoinSiteMessageService messageService;
    @Autowired
    private SundayProductCategoryService categoryService;
    @Autowired
    private SundaySiteAdvertiseMentService advertiseMentService;
    @Autowired
    private JoinSitePictureService pictureService;
    @Autowired
    private JoinSiteCountService countService;

    /**
     * 单个项目主页
     * @param request
     * @param response
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public String  findOne(HttpServletRequest request,
                         HttpServletResponse response, Model model,
                         @RequestParam(value="id",required=true)Long id ,
                         @RequestParam(value="businessId",required=false)Long businessId
                         ){
        //加载页面通用
        countService.count(model);
        if(LongUtil.isNotZreo(businessId)){
            id=productService.findOneByMember(businessId).getId();
        }
        Map<String,Object> param=new HashMap<String,Object>();
        SundayProduct product=productService.find(id);
        //增加点击量
        product.setClickNum(product.getClickNum()+1);
        productService.update(product);
        //组装标签
        productService.packageTags(product,true);
        model.addAttribute("product",product);
        Long memberId=permissionService.getMember(product.getId());
        SundayMember siteBusiness=memberService.getById(memberId);
        model.addAttribute("siteBusiness",siteBusiness);
        //加载留言5条
        param.clear();
        param.put("isDeleted",0);
        param.put("productId",id);
        List<JoinMessage> messages=messageService.list(param,5);
        model.addAttribute("messages",messages);
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,product.getParentId());
        model.addAttribute("paimingProducts",paimingProducts);
        //获取好项目
        List<SundayProduct> goodProducts=productService.selectGood(product.getParentId());
        model.addAttribute("goodProducts",goodProducts);
        //获取相关项目4个
        List<SundayProduct> xiangguanProducts=productService.selectXiangguan(id);
        model.addAttribute("xiangguanProducts",xiangguanProducts);
        //加载项目图片
        List<JoinPicture> pictures=pictureService.selectRoll(request,siteBusiness.getRecordId());
        model.addAttribute("pictures",pictures);
        JoinPicture backPicture=pictureService.selectBack(request,siteBusiness.getRecordId());
        model.addAttribute("backPicture",backPicture);
        return "/join/site/product/findOne";
    }
    @RequestMapping("/jieshao")
    public String  jieshao(HttpServletRequest request,
                           HttpServletResponse response, Model model,
                           @RequestParam(value="id",required=true)Long id ,
                           @RequestParam(value="businessId",required=false)Long businessId
    ){
        //加载页面通用
        countService.count(model);
        if(LongUtil.isNotZreo(businessId)){
            id=productService.findOneByMember(businessId).getId();
        }
        Map<String,Object> param=new HashMap<String,Object>();
        SundayProduct product=productService.find(id);
        //增加点击量
        product.setClickNum(product.getClickNum()+1);
        productService.update(product);
        //组装标签
        productService.packageTags(product,true);
        model.addAttribute("product",product);
        Long memberId=permissionService.getMember(product.getId());
        SundayMember siteBusiness=memberService.getById(memberId);
        model.addAttribute("siteBusiness",siteBusiness);
        //加载留言5条
        param.clear();
        param.put("isDeleted",0);
        param.put("productId",id);
        List<JoinMessage> messages=messageService.list(param,5);
        model.addAttribute("messages",messages);
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,product.getParentId());
        model.addAttribute("paimingProducts",paimingProducts);
        //获取好项目
        List<SundayProduct> goodProducts=productService.selectGood(product.getParentId());
        model.addAttribute("goodProducts",goodProducts);
        //获取相关项目4个
        List<SundayProduct> xiangguanProducts=productService.selectXiangguan(id);
        model.addAttribute("xiangguanProducts",xiangguanProducts);
        //加载项目图片
        List<JoinPicture> pictures=pictureService.selectRoll(request,siteBusiness.getRecordId());
        model.addAttribute("pictures",pictures);
        JoinPicture backPicture=pictureService.selectBack(request,siteBusiness.getRecordId());
        model.addAttribute("backPicture",backPicture);
        return "/join/site/product/jieshao";
    }
    @RequestMapping("/feiyong")
    public String  feiyong(HttpServletRequest request,
                           HttpServletResponse response, Model model,
                           @RequestParam(value="id",required=true)Long id ,
                           @RequestParam(value="businessId",required=false)Long businessId
    ){
        //加载页面通用
        countService.count(model);
        if(LongUtil.isNotZreo(businessId)){
            id=productService.findOneByMember(businessId).getId();
        }
        Map<String,Object> param=new HashMap<String,Object>();
        SundayProduct product=productService.find(id);
        //增加点击量
        product.setClickNum(product.getClickNum()+1);
        productService.update(product);
        //组装标签
        productService.packageTags(product,true);
        model.addAttribute("product",product);
        Long memberId=permissionService.getMember(product.getId());
        SundayMember siteBusiness=memberService.getById(memberId);
        model.addAttribute("siteBusiness",siteBusiness);
        //加载留言5条
        param.clear();
        param.put("isDeleted",0);
        param.put("productId",id);
        List<JoinMessage> messages=messageService.list(param,5);
        model.addAttribute("messages",messages);
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,product.getParentId());
        model.addAttribute("paimingProducts",paimingProducts);
        //获取好项目
        List<SundayProduct> goodProducts=productService.selectGood(product.getParentId());
        model.addAttribute("goodProducts",goodProducts);
        //获取相关项目4个
        List<SundayProduct> xiangguanProducts=productService.selectXiangguan(id);
        model.addAttribute("xiangguanProducts",xiangguanProducts);
        //加载项目图片
        List<JoinPicture> pictures=pictureService.selectRoll(request,siteBusiness.getRecordId());
        model.addAttribute("pictures",pictures);
        JoinPicture backPicture=pictureService.selectBack(request,siteBusiness.getRecordId());
        model.addAttribute("backPicture",backPicture);
        return "/join/site/product/feiyong";
    }

    @RequestMapping("/youshi")
    public String  youshi(HttpServletRequest request,
                           HttpServletResponse response, Model model,
                           @RequestParam(value="id",required=true)Long id ,
                           @RequestParam(value="businessId",required=false)Long businessId
    ){
        //加载页面通用
        countService.count(model);
        if(LongUtil.isNotZreo(businessId)){
            id=productService.findOneByMember(businessId).getId();
        }
        Map<String,Object> param=new HashMap<String,Object>();
        SundayProduct product=productService.find(id);
        //增加点击量
        product.setClickNum(product.getClickNum()+1);
        productService.update(product);
        //组装标签
        productService.packageTags(product,true);
        model.addAttribute("product",product);
        Long memberId=permissionService.getMember(product.getId());
        SundayMember siteBusiness=memberService.getById(memberId);
        model.addAttribute("siteBusiness",siteBusiness);
        //加载留言5条
        param.clear();
        param.put("isDeleted",0);
        param.put("productId",id);
        List<JoinMessage> messages=messageService.list(param,5);
        model.addAttribute("messages",messages);
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,product.getParentId());
        model.addAttribute("paimingProducts",paimingProducts);
        //获取好项目
        List<SundayProduct> goodProducts=productService.selectGood(product.getParentId());
        model.addAttribute("goodProducts",goodProducts);
        //获取相关项目4个
        List<SundayProduct> xiangguanProducts=productService.selectXiangguan(id);
        model.addAttribute("xiangguanProducts",xiangguanProducts);
        //加载项目图片
        List<JoinPicture> pictures=pictureService.selectRoll(request,siteBusiness.getRecordId());
        model.addAttribute("pictures",pictures);
        JoinPicture backPicture=pictureService.selectBack(request,siteBusiness.getRecordId());
        model.addAttribute("backPicture",backPicture);
        return "/join/site/product/youshi";
    }
    /**
     * 单个项目详情页
     * @param request
     * @param response
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public String  detail(HttpServletRequest request,
                         HttpServletResponse response, Model model,
                         @RequestParam(value="id",required=true)Long id ,
                         @RequestParam(value="businessId",required=false)Long businessId
    ){
        //加载页面通用
        countService.count(model);
        if(LongUtil.isNotZreo(businessId)){
            id=productService.findOneByMember(businessId).getId();
        }
        Map<String,Object> param=new HashMap<String,Object>();
        SundayProduct product=productService.findOne(id);
        model.addAttribute("product",product);
        Long memberId=permissionService.getMember(product.getId());
        SundayMember siteBusiness=memberService.getById(memberId);
        model.addAttribute("siteBusiness",siteBusiness);
        //加载留言5条
        param.clear();
        param.put("isDeleted",0);
        param.put("productId",id);
        List<JoinMessage> messages=messageService.list(param,5);
        model.addAttribute("messages",messages);

        return "/join/site/product/detail";
    }
    /**
     * 加载评论页面
     * @param request
     * @param response
     * @param model
     * @param id
     * @param page
     * @return
     */
    @RequestMapping("/more")
    public String  more(HttpServletRequest request,
                         HttpServletResponse response, Model model,
                         @RequestParam(value="id",required=true)Long id ,
                        @RequestParam(value="page",required=false)Integer page){
        //加载页面通用
        countService.count(model);
        //加载常用搜索关键词
        List<SundayAdvertisementDto> common_searchs =advertiseMentService.getAdvertisementByZone("common_search");
        model.addAttribute("common_searchs",common_searchs);
        Map<String,Object> param=new HashMap<String,Object>();
        SundayProduct product=productService.find(id);
        productService.packageTags(product,true);
        model.addAttribute("product",product);
        Long memberId=permissionService.getMember(product.getId());
        model.addAttribute("siteBusiness",memberService.getById(memberId));
        //获取排名项目
        List<SundayProduct> paimingProducts=productService.selectPaiming(10,product.getParentId());
        model.addAttribute("paimingProducts",paimingProducts);
        //获取好项目
        List<SundayProduct> goodProducts=productService.selectGood(product.getParentId());
        model.addAttribute("goodProducts",goodProducts);
        //获取相关项目4个
        List<SundayProduct> xiangguanProducts=productService.selectXiangguan(id);
        model.addAttribute("xiangguanProducts",xiangguanProducts);


        PageResult<JoinMessage> result=messageService.selectMoreMessage(request,product.getId());
        model.addAttribute("messages",result.getData());

        model.addAttribute("currentPage",page);
        model.addAttribute("totalPage",result.getTotal()/10+1);


        return "/join/site/product/more";
    }



    /**
     * 根据分类分组   获取前六个数据
     * @param request
     * @param response
     */
    @RequestMapping("/selectNewSix")
    public void selectNewSix(HttpServletRequest request,
                           HttpServletResponse response
    ){
        try{
            List<SundayProduct> products=productService.selectNewSix();
            AjaxUtils.renderJson(response,products);
        }catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/site/product/selectNewSix------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 获取品牌优选前六个
     * @param request
     * @param response
     * @param categoryIds
     */
    @RequestMapping("/selectGoodSix")
    public void selectGoodSix(HttpServletRequest request,
                             HttpServletResponse response,
                              @RequestParam(value="categoryIds",required=true)List<Long> categoryIds
    ){
        try{
            List<SundayProduct> products=productService.selectGoodSix(categoryIds);
            AjaxUtils.renderJson(response,products);
        }catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/site/product/selectGoodSix------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }
}
